Skip to content

feat: implement CEP-47 server redirect client/server middleware and re-issuance - #78

Open
abhayguptas wants to merge 7 commits into
ContextVM:masterfrom
abhayguptas:feat/cep-47-redirect
Open

feat: implement CEP-47 server redirect client/server middleware and re-issuance#78
abhayguptas wants to merge 7 commits into
ContextVM:masterfrom
abhayguptas:feat/cep-47-redirect

Conversation

@abhayguptas

Copy link
Copy Markdown
Contributor

Description

This PR implements CEP-47 Server Redirect support for @contextvm/sdk, adding full end-to-end middleware and re-issuance capabilities. It faithfully follows the mentor's callback-driven middleware design without mutating the stateless initialization lifecycle, and allows the client to transparently handle complex chained redirects with a safety hop cap.

Features Included

  1. Server-Side Redirect (withServerRedirect):
    • A middleware that accepts a resolveRedirect callback.
    • It is decoupled from state configuration (e.g., stateless mode). It evaluates the incoming JSON-RPC request and if a redirect is needed, emits the standard -32044 MCP error payload with the target pubkey and relays.
  2. Client-Side Redirect (withClientRedirect):
    • Intercepts incoming messages looking for the -32044 error.
    • Transparently handles the redirect by spinning up a new NostrClientTransport to the target server.
    • Properly routes subsequent messages and replaces the old transport for a seamless caller experience.
    • Includes cycle/loop detection with a customizable maxRedirects option (defaulting to 5 hops).
  3. Bug Fixes:
    • Fixed a bug in ApplesauceRelayPool message handling. applesauce-relay emits raw events rather than typed wrapper objects in the req() subscription. Fixed the message handler so that subscriptions correctly fire on events.
    • Fixed a double prepending issue in mock-relay-server connection mapping which caused test relay timeouts.
  4. Integration & Proxy:
    • withClientRedirect is cleanly hooked up into NostrMCPProxy alongside payments.
    • Lint rules pass and TypeScript is fully typed.

Testing

  • End-to-end single redirect (Client -> Server A -> Server B -> responds)
  • Chained redirects (Client -> Server A -> Server B -> Server C -> responds)
  • Infinite Loop detection (Client -> A -> B -> A -> Error thrown via McpError -32044)

All unit tests and E2E integration tests are passing successfully.

Changeset

Included a changeset for the upcoming release.

@abhayguptas
abhayguptas force-pushed the feat/cep-47-redirect branch from 8453650 to fc04859 Compare July 29, 2026 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant